package com.project.controller;

import com.project.model.entity.ReportContent;
import com.project.result.Result;
import com.project.service.ReportContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reportContent")
public class ReportContentController {
    private final ReportContentService reportContentService;

    @Autowired
    public ReportContentController(ReportContentService reportContentService) {
        this.reportContentService = reportContentService;
    }

    @PostMapping("/saveOrUpdate")
    public Result<ReportContent> saveOrUpdateReportContent(@RequestBody ReportContent reportContent) {
        try {
            if (reportContent == null) {
                return Result.fail("报告内容为空");
            }

            if (reportContentService.getReportContentById(reportContent.getId()) == null) {
                ReportContent savedContent = reportContentService.addReportContent(reportContent);
                return savedContent != null ? Result.ok(savedContent) : Result.fail("保存失败");
            } else {
                ReportContent updatedContent = reportContentService.updateReportContent(reportContent);
                return updatedContent != null ? Result.ok(updatedContent) : Result.fail("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }


    @DeleteMapping("/delete")
    public Result<Void> deleteReportContent(@RequestParam Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            reportContentService.deleteReportContent(id);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("/getById")
    public Result<ReportContent> getReportContentById(@RequestParam Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            ReportContent reportContent = reportContentService.getReportContentById(id);
            return reportContent != null ? Result.ok(reportContent) : Result.fail("未找到该报告内容");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("/getAll")
    public Result<List<ReportContent>> getAllReportContents() {
        try {
            List<ReportContent> reportContents = reportContentService.getAllReportContents();
            return !reportContents.isEmpty() ? Result.ok(reportContents) : Result.fail("没有数据");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("/getByReportId")
    public Result<List<ReportContent>> getReportContentsByReportId(@RequestParam Integer reportId) {
        try {
            if (reportId == null || reportId <= 0) {
                return Result.fail("报告ID无效");
            }
            List<ReportContent> reportContents = reportContentService.getReportContentsByReportId(reportId);
            return !reportContents.isEmpty() ? Result.ok(reportContents) : Result.fail("未找到报告内容");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }
}

